package com.zhongying.jf.util;

import net.sf.json.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;

/**
 * @author liyuxuan
 * @Description
 * @Email liyuxuan@zhongyingjinrong.com
 * Created on 2017/5/6 下午7:05
 * Copyright (c) 2017 All Rights Reserved.
 */
public class CalcMobileCity {


    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CalcMobileCity.class);
    public static void main(String[] args) {
        try {
            calcMobileCity("17187460928");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试手机号码是来自哪个城市的，利用拍拍网的API
     *
     * @param mobileNumber 手机号码
     * @return
     * @throws MalformedURLException
     */
    public static String calcMobileCity(String mobileNumber) throws Exception {
        String province=null;
        StringBuffer sb = new StringBuffer("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm");
        sb.append("?tel=").append(mobileNumber);
        String s = sendGet2(sb.toString());
        LOGGER.error("调淘宝的1s="+s);

        //截图字符串并且解析
        String jieguo = s.substring(s.indexOf("=")+1,s.length());
        LOGGER.error("调淘宝的2jieguo="+jieguo);
        String jieguo1 =jieguo+"}";
        LOGGER.error("调淘宝的2jieguo1="+jieguo1);
        JSONObject jsonObject = JSONObject.fromObject(jieguo1);
        LOGGER.error("调淘宝的2jsonObject="+jsonObject);
        if(jsonObject.isEmpty()){
            province="中国";
            return province;
        }
        province = jsonObject.get("province").toString();
        LOGGER.error("调淘宝的2provincet="+province);
        return province;
    }

    public static String sendGet2(String urlNameString)  {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String content =null;
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(urlNameString);

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            try {
                response = httpclient.execute(httpGet);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                try {
                    content = EntityUtils.toString(response.getEntity(), "UTF-8");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("内容：" + content);
            }
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return content;
    }
}
